package com.ruoyi.app.controller.activity;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.app.domain.AppActivityReg;
import com.ruoyi.app.mapper.AppActivityRegMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.app.domain.AppActivity;
import com.ruoyi.app.service.IAppActivityService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 活动信息Controller
 * 
 * @author ruoyi
 * @date 2025-03-19
 */
@RestController
@RequestMapping("/system/activity")
public class AppActivityController extends BaseController
{
    @Autowired
    private IAppActivityService appActivityService;
    @Autowired
    private AppActivityRegMapper appActivityRegMapper;


    @GetMapping("/listForCenter")
    public TableDataInfo listForCenter(AppActivity appActivity)
    {
        startPage();
        appActivity.setStatus("1");
        List<AppActivity> list = appActivityService.selectAppActivityList(appActivity);
        if (CollectionUtil.isNotEmpty(list)) {
            for (AppActivity activity : list) {
                activity.setCurrCount(appActivityRegMapper.selectCount(new LambdaQueryWrapper<AppActivityReg>().eq(AppActivityReg::getActivityId, activity.getActivityId())));
            }
        }
        return getDataTable(list);
    }
    /**
     * 查询活动信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AppActivity appActivity)
    {
        startPage();
        List<AppActivity> list = appActivityService.selectAppActivityList(appActivity);
        return getDataTable(list);
    }

    /**
     * 导出活动信息列表
     */
    @Log(title = "活动信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppActivity appActivity)
    {
        List<AppActivity> list = appActivityService.selectAppActivityList(appActivity);
        ExcelUtil<AppActivity> util = new ExcelUtil<AppActivity>(AppActivity.class);
        util.exportExcel(response, list, "活动信息数据");
    }

    /**
     * 获取活动信息详细信息
     */
    @GetMapping(value = "/{activityId}")
    public AjaxResult getInfo(@PathVariable("activityId") Long activityId)
    {
        return success(appActivityService.selectAppActivityByActivityId(activityId));
    }

    /**
     * 新增活动信息
     */
    @Log(title = "活动信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppActivity appActivity)
    {
        return toAjax(appActivityService.insertAppActivity(appActivity));
    }

    /**
     * 修改活动信息
     */
    @Log(title = "活动信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppActivity appActivity)
    {
        return toAjax(appActivityService.updateAppActivity(appActivity));
    }

    /**
     * 删除活动信息
     */
    @Log(title = "活动信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{activityIds}")
    public AjaxResult remove(@PathVariable Long[] activityIds)
    {
        return toAjax(appActivityService.deleteAppActivityByActivityIds(activityIds));
    }

    @Log(title = "活动信息", businessType = BusinessType.UPDATE)
    @PostMapping("/audit")
    public AjaxResult audit(@RequestBody AppActivity appActivity)
    {
        return toAjax(appActivityService.auditActivity(appActivity));
    }
}
